package com.yao.seckill.web.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;

/**
 * Copyright (C), 2022-2022, 姚兵
 * Author: 32210
 * Date: 2022/9/13 21:11
 * FileName: EsConfig
 * Description: es
 */
@Configuration
public class EsConfig {

   @Bean
    public ElasticsearchClient elasticsearchClient(ElasticsearchProperties elasticsearchProperties) throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException {

//       Path caCertificatePath = Paths.get("E:\\http_ca.crt");
//       CertificateFactory factory =
//               CertificateFactory.getInstance("X.509");
//       Certificate trustedCa;
//       try (InputStream is = Files.newInputStream(caCertificatePath)) {
//           trustedCa = factory.generateCertificate(is);
//       }
//       KeyStore trustStore = KeyStore.getInstance("pkcs12");
//       trustStore.load(null, null);
//       trustStore.setCertificateEntry("ca", trustedCa);
//       SSLContextBuilder sslContextBuilder = SSLContexts.custom()
//               .loadTrustMaterial(trustStore, null);
//       final SSLContext sslContext = sslContextBuilder.build();
       final CredentialsProvider credentialsProvider =
               new BasicCredentialsProvider();
       credentialsProvider.setCredentials(AuthScope.ANY,
               new UsernamePasswordCredentials(elasticsearchProperties.getUsername(), elasticsearchProperties.getPassword()));
       // Create the low-level client
       RestClient restClient = RestClient.builder(
               new HttpHost("es9200", 9200,"http"))
               .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                   @Override
                   public HttpAsyncClientBuilder customizeHttpClient(
                           HttpAsyncClientBuilder httpClientBuilder) {
                       return httpClientBuilder
                               .setDefaultCredentialsProvider(credentialsProvider);
                   }
               }).build();


       // Create the transport with a Jackson mapper
       ElasticsearchTransport transport = new RestClientTransport(
               restClient, new JacksonJsonpMapper());

       // And create the API client
       ElasticsearchClient client = new ElasticsearchClient(transport);
        return client;
   }


}
